/**
 * 
 */
package View;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.io.File;
import java.util.ArrayList;

import javax.swing.*;
import javax.swing.filechooser.FileSystemView;

import Model.contact;
/**
 * @author enzo
 *
 */
public class Form extends JPanel {
	
	private JPanel centerPanel;
	private JPanel picPanel;
	private JButton createContact;
	private JButton saveAlter;
	private JButton browse;
	private JPanel previewPan;
	// TextField components of the form
	private JTextField textName;
	private JTextField textFirstname;
	private JTextField textAddress;
	private JTextField textCompanyAddress;
	private JTextField textPhoneperso; // index 0 fixe , index 1 mobile
	private JTextField textPhonepro; // index 0 office , index 1 mobile
	private JTextField textURL;
	private JTextField textEmailPerso;
	private JTextField textEmailPro;
	private JTextField textPhoto;
	private JTextField textGroup;
	private JFileChooser fileChooser;
	
	//Label going with each of those TextFields
	private JLabel labelName;
	private JLabel labelFirstname;
	private JLabel labelAddress;
	private JLabel labelCompanyAddress;
	private JLabel labelPhoneperso; // index 0 fixe , index 1 mobile
	private JLabel labelPhonepro; // index 0 office , index 1 mobile
	private JLabel labelURL;
	private JLabel labelEmailPerso;
	private JLabel labelEmailPro;
	private JLabel labelPhoto;
	private JLabel labelGroup;
	private JLabel preview;
	/**
	 * Form constructor
	 */
	public Form()
	{
		centerPanel= new JPanel();
		picPanel=new JPanel();
		
		labelName= new JLabel("Name : ",SwingConstants.CENTER);
		textName=new JTextField();
		labelName.setLabelFor(textName);
		labelFirstname= new JLabel("FirstName : ",SwingConstants.CENTER);
		textFirstname=new JTextField();
		labelFirstname.setLabelFor(textFirstname);
		labelAddress= new JLabel("Address : ",SwingConstants.CENTER);
		textAddress=new JTextField();
		labelAddress.setLabelFor(textAddress);
		labelCompanyAddress= new JLabel("Company Address : ",SwingConstants.CENTER);
		textCompanyAddress=new JTextField();
		labelCompanyAddress.setLabelFor(textCompanyAddress);
		labelEmailPerso= new JLabel("email perso : ",SwingConstants.CENTER);
		textEmailPerso=new JTextField();
		labelEmailPerso.setLabelFor(textEmailPerso);
		labelEmailPro= new JLabel("email pro  : ",SwingConstants.CENTER);
		textEmailPro=new JTextField();
		labelEmailPro.setLabelFor(textEmailPro);
		labelPhoneperso= new JLabel("Personal Phone Number : ",SwingConstants.CENTER);
		textPhoneperso=new JTextField();
		labelPhoneperso.setLabelFor(textPhoneperso);
		labelPhonepro= new JLabel("Professional phone number : ",SwingConstants.CENTER);
		textPhonepro=new JTextField();
		labelPhonepro.setLabelFor(textPhonepro);
		labelURL= new JLabel("Website : ",SwingConstants.CENTER);
		labelURL.setLabelFor(textURL);
		textURL=new JTextField();
		labelPhoto= new JLabel("Picture : ",SwingConstants.CENTER);
		textPhoto=new JTextField();
		labelPhoto.setLabelFor(textPhoto);
		labelGroup= new JLabel("Group : ",SwingConstants.CENTER);
		textGroup= new JTextField();
		createContact= new JButton("Create Contact");
		saveAlter=new JButton("Save Changes");
		browse=new JButton("Browse");
		preview=new JLabel();
		previewPan=new JPanel();
		previewPan.add(preview);
		
		picPanel.setLayout(new GridLayout(1,2));
		picPanel.add(textPhoto);
		picPanel.add(browse);
		
		centerPanel.setLayout(new GridLayout(11,2,0,10));
		centerPanel.add(labelName);
		centerPanel.add(textName);
		centerPanel.add(labelFirstname);
		centerPanel.add(textFirstname);
		centerPanel.add(labelAddress);
		centerPanel.add(textAddress);
		centerPanel.add(labelCompanyAddress);
		centerPanel.add(textCompanyAddress);
		centerPanel.add(labelEmailPerso);
		centerPanel.add(textEmailPerso);
		centerPanel.add(labelEmailPro);
		centerPanel.add(textEmailPro);
		centerPanel.add(labelPhoneperso);
		centerPanel.add(textPhoneperso);
		centerPanel.add(labelPhonepro);
		centerPanel.add(textPhonepro);
		centerPanel.add(labelURL);
		centerPanel.add(textURL);
		centerPanel.add(labelPhoto);
		centerPanel.setVisible(true);
		centerPanel.add(picPanel);
		centerPanel.add(labelGroup);
		centerPanel.add(textGroup);
		FileSystemView systemView=FileSystemView.getFileSystemView();
		
		fileChooser = new JFileChooser(new File(systemView.getHomeDirectory().getAbsolutePath()+"/images"));
		fileChooser.setAccessory(previewPan);
		
		this.setLayout(new BorderLayout());
		this.add("Center",centerPanel);
		this.add("South",createContact);
		this.setVisible(true);
		
		
	}

	/**
	 * @return the createContact
	 */
	public JButton getCreateContact() {
		return createContact;
	}
	public ArrayList<String> retrieveFormInfo()
	{
		ArrayList<String> info =new ArrayList <String>();
		info.add(textName.getText());
		info.add(textFirstname.getText());
		info.add(textAddress.getText());
		info.add(textCompanyAddress.getText());
		info.add(textPhoneperso.getText());
		info.add(textPhonepro.getText());
		info.add(textEmailPerso.getText());
		info.add(textEmailPro.getText());
		info.add(textURL.getText());
		info.add(textPhoto.getText());
		info.add(textGroup.getText());
		return info;
		
	}
	public void cleanForm()
	{
		textName.setText("");
		textFirstname.setText("");
		textAddress.setText("");
		textCompanyAddress.setText("");
		textPhoneperso.setText("");
		textPhonepro.setText("");
		textURL.setText("");
		textEmailPerso.setText("");
		textEmailPro.setText("");
		textPhoto.setText("");
		textGroup.setText("");
		this.remove(saveAlter);
		this.add("South",createContact);
	}
	public void AlterForm(contact c)
	{
		textName.setText(c.getName());
		textFirstname.setText(c.getFirstname());
		textAddress.setText(c.getAddress());
		textCompanyAddress.setText(c.getCompanyAddress());
		textPhoneperso.setText(c.getPhoneperso());
		textPhonepro.setText(c.getPhonepro());
		textURL.setText(c.getURL());
		textEmailPerso.setText(c.getEmailPerso());
		textEmailPro.setText(c.getEmailPro());
		textPhoto.setText(c.getPhoto());
		textGroup.setText(c.getGroup());
		this.remove(createContact);
		this.add("South",saveAlter);
	}

	/**
	 * @return the saveAlter
	 */
	public JButton getSaveAlter() {
		return saveAlter;
	}

	/**
	 * @return the fileChooser
	 */
	public JFileChooser getFileChooser() {
		return fileChooser;
	}

	/**
	 * @return the browse
	 */
	public JButton getBrowse() {
		return browse;
	}
	public void browse(){
		
		File fichier= fileChooser.getSelectedFile();
		textPhoto.setText(fichier.getPath());
	}

	/**
	 * @return the preview
	 */
	public JLabel getPreview() {
		return preview;
	}
	public void preview()
	{
		if(fileChooser.getSelectedFile()!=null) 
		if(fileChooser.getSelectedFile().getPath().endsWith(".png") || fileChooser.getSelectedFile().getPath().endsWith(".jpg") ||fileChooser.getSelectedFile().getPath().endsWith(".gif") || fileChooser.getSelectedFile().getPath().endsWith(".bmp"))
			{
			ImageIcon icon=new ImageIcon(fileChooser.getSelectedFile().getAbsolutePath());
			icon.setImage(icon.getImage().getScaledInstance(200, 200, Image.SCALE_DEFAULT));
			preview.setIcon(icon);
			
			}
	}


}
